import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_frame/assets/images/mine/model/mine_model.dart';
import 'package:flutter_frame/common/user_manager.dart';
import 'package:flutter_frame/utils/app_util.dart';
import 'package:flutter_frame/utils/theme_util.dart';
import 'package:flutter_frame/utils/toast_utils.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../../assets/color_config.dart';
import '../../common/common_config.dart';
import '../../common/router.dart';
import '../../weiget/button_widget.dart';
import '../../weiget/flutter_dialog.dart';
import '../../weiget/image_widget.dart';

class MinePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => MinePageState();
}

class MinePageState extends State {
  var count = 1.obs;
  List<MineModel> dataSource= [];
 
  @override
  void initState() {
    super.initState();
    loadData();
  }
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: ColorConfig.colorWhite,
      appBar: AppBar(
        title: Text(
          'mine'.tr,
          style: TextStyle(
              fontWeight: FontWeight.bold,
              fontSize: 18.sp,
              color: ColorConfig.colorWhite),
        ),
        backgroundColor: ColorConfig.colorPrimary,
        systemOverlayStyle: ThemeUtil.light,
      ),
      extendBodyBehindAppBar: false,
      body: Container(
        color: ColorConfig.colorF5,
        child: CustomScrollView(
          slivers: [
            SliverToBoxAdapter(
              child: topWidget(),
            ),
            SliverFixedExtentList(
                delegate: SliverChildBuilderDelegate(
                  (BuildContext context, int index) {
                    return mineListWidget(index);
                  },
                  childCount: dataSource.length,
                ),
                itemExtent: 60.w),
            SliverToBoxAdapter(
              child: bottomWidget(),
            ),
          ],
        ),
      ),
    );
  }

  ///获取数据
  loadData() async {
    dataSource = await AppUtil.getMineJsonData();
    setState(() {});
  }

  topWidget() {
    return Container(
        child: Stack(
      clipBehavior: Clip.none,
      children: [
        Container(
          height: 50.h,
          color: ColorConfig.colorPrimary,
        ),
        Container(
          height: 100.h,
          margin: EdgeInsets.symmetric(horizontal: 10.w),
          child: Card(
              shape: RoundedRectangleBorder(
                  side: BorderSide(color: Colors.black, width: 3.w),
                  borderRadius: BorderRadius.all(Radius.circular(15.w))),
              color: ColorConfig.colorWhite,
              shadowColor: ColorConfig.colorBlack,
              elevation: 4.w,
              child: Container(
                padding: EdgeInsets.symmetric(horizontal: 10.w),
                child: Row(
                  children: [
                    ImageWidget(
                      name: "${assetImage}mine/mine_user.png",
                      width: 60.w,
                      height: 60.w,
                    ),
                    SizedBox(width: 10.w),
                    Text(
                      '****6497',
                      style: TextStyle(
                          fontSize: 16.sp, color: ColorConfig.colorPrimary),
                    )
                  ],
                ),
              )),
        ),
      ],
    ));
  }
  
  mineListWidget(int index){
    MineModel model = dataSource[index];
    return ButtonWidget(
      onPressed: (){
        MineModel model = dataSource[index];
        if (model.id == "face") {
          //人脸
          Get.toNamed(Routers.face);
        } else if (model.id == "bank") {
          showToast('develop...');
        } else {
          //其他
          Get.toNamed(model.router, arguments: model);
        }
      },
      child: Column(
        children: [
          ListTile(
            contentPadding:
            EdgeInsets.only(left: 15.w, right: 15.w, top: 0, bottom: 0),
            minVerticalPadding: 0,
            leading: SizedBox(
              width: 30.w,
              height: 30.w,
              child: ImageWidget(
                name: "${assetImage}mine/${model.icon}.png",
                fit: BoxFit.fitWidth,
              ),
            ),
            trailing: SizedBox(
              width: 55.w,
              height: 20.w,
              child: const Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  ImageWidget(
                    name: "${assetImage}base/arrowright_black.png",
                    fit: BoxFit.fitWidth,
                  ),
                ],
              ),
            ),
            horizontalTitleGap: 1.w,
            title: Text(
              model.title.tr,
              style: TextStyle(
                color: ColorConfig.colorPrimary,
                fontSize: 14.sp,
              ),
            ),
          ),
          Divider(
            height: 1.w,
            color: const Color.fromRGBO(200, 200, 200, 1),
          ),
        ],
      ),
    );
  }

  bottomWidget(){
    return Container(
      color: ColorConfig.colorWhite,
      width: double.infinity,
      margin: EdgeInsets.only(left: 25.w, right: 25.w, top: 20.w, bottom: 30.w),
      child: ButtonWidget(
        onPressed: (){
          exitLoginAction();
        },
        child: Container(
          width: double.infinity,
          height: 40.w,
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.all(
              Radius.circular(5.w),
            ),
          ),
          child: Center(
            child: Text(
              "tv_logout_title".tr,
              style: TextStyle(
                color: Colors.black,
                fontSize: 16.sp,
                fontWeight: FontWeight.bold,
              ),
            ),
          ),
        ),
      ),
    );
  }

  ///点击退出登陆
  exitLoginAction() {
    FlutterDialog.show(context,
        title: "tv_logout_title".tr,
        content: "tv_logout_content".tr,
        cancel: "title_cancel".tr,
        ok: "title_ok".tr, cancelFun: () {
          Navigator.of(context).pop();
        }, okFun: () {
          UserManager().logout();
          Get.offAllNamed(Routers.login);
        });
  }


}
